package com.kedacom.freedomLabel.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kedacom.freedomLabel.R;


/**
 * Author：李程
 * CreateTime：2018/6/5 18:02
 * E-mail：licheng@kedacom.com
 * Describe：表单条目
 */
public class NoteItemView extends RelativeLayout {

    int type;
    TextView nameView;
    EditText inputView;
    TextView contentView;
    ImageView arrowView;

    public NoteItemView(Context context) {
        super(context);
    }

    public NoteItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NoteItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        View view = LayoutInflater.from(context).inflate(R.layout.item_note_layout, this);
        nameView = view.findViewById(R.id.item_note_name);
        // 获取属性值
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.NoteItemView);
        String name;
        String hint;
        String content;
        try {
            type = array.getInt(R.styleable.NoteItemView_note_type, 1);
            name = array.getString(R.styleable.NoteItemView_note_name);
            hint = array.getString(R.styleable.NoteItemView_note_content_hint);
            content = array.getString(R.styleable.NoteItemView_text);
            if (name == null)
                name = "";
            if (hint == null)
                hint = "";
            if (content == null)
                content = "";
            nameView.setText(name);
            if (type == 1) {
                view.findViewById(R.id.item_note_auto_layout).setVisibility(View.VISIBLE);
                contentView = view.findViewById(R.id.item_note_auto_content);
                contentView.setHint(hint);
            } else if (type == 2) {
                view.findViewById(R.id.item_note_input_layout).setVisibility(View.VISIBLE);
                inputView = view.findViewById(R.id.item_note_input);
                inputView.setHint(hint);
                int inputType = array.getInt(R.styleable.NoteItemView_note_input_type, 2);
                if (inputType == 1) {
                    inputView.setInputType(InputType.TYPE_CLASS_NUMBER);
                } else if (inputType == 2) {
                    inputView.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE);
                } else if (inputType == 3) {
                    inputView.setInputType(InputType.TYPE_CLASS_PHONE);
                } else if (inputType == 4) {
                    inputView.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                }
            } else if (type == 3) {
                view.findViewById(R.id.item_note_select_layout).setVisibility(View.VISIBLE);
                contentView = view.findViewById(R.id.item_note_select_content);
                contentView.setHint(hint);
                arrowView = view.findViewById(R.id.item_note_arrow);
            } else {
                view.findViewById(R.id.item_note_auto_layout).setVisibility(View.VISIBLE);
                contentView = view.findViewById(R.id.item_note_auto_content);
                contentView.setHint(hint);
            }
            setText(content);
        } finally {
            array.recycle();
        }

    }

    public void setText(@Nullable CharSequence content) {
        setContentText(content);
    }

    /**
     * 获取列名
     *
     * @return 列名
     */
    public String getItemName() {
        if (this.nameView == null)
            return "";
        return this.nameView.getText().toString();
    }

    public void setItemName(@NonNull CharSequence name) {
        if (nameView != null)
            nameView.setText(name);
    }

    public boolean isContentEmpty() {
        String text = getContentText();
        return text.isEmpty();
    }

    public String getContentText() {
        if (this.type == 1 || this.type == 3) {
            return this.contentView.getText().toString().trim();
        }
        if (this.type == 2) {
            return this.inputView.getText().toString().trim();
        }
        return "";
    }

    public void setContentText(@Nullable CharSequence content) {
        if (this.type == 1 || this.type == 3) {
            if (contentView != null)
                contentView.setText(content);
        } else if (this.type == 2) {
            if (inputView != null) {
                inputView.setText(content);
                inputView.setSelection(content == null ? 0 : content.length());
            }
        }
    }

    public void setHintText(@Nullable CharSequence content) {
        if (this.type == 1 || this.type == 3) {
            if (contentView != null)
                contentView.setHint(content);
        } else if (this.type == 2) {
            if (inputView != null) {
                inputView.setHint(content);
            }
        }
    }

    public String getContentHintText() {
        if (this.type == 1 || this.type == 3) {
            return this.contentView.getHint().toString().trim();
        }
        if (this.type == 2) {
            return this.inputView.getHint().toString().trim();
        }
        return "";
    }


    public EditText getInputView(){
        return inputView;
    }
}
